library("tidyverse")
library("ggplot2")
library("sf")
library("dplyr")
library("lubridate")
library("ggbreak")
library("plotly")
library("ggrepel")
library("htmlwidgets")
library("directlabels")
library("rgdal")
library("httr")
library("geojsonsf")
library("leaflet")
library("leaflet.extras")
library("viridis")
Make custom palettes and theme
palette1= c("#FC4E2A","#BD0026")
palette2 = c("#045A8D","#3690C0")
palette3= c("#FC4E2A","#045A8D","#BD0026","#3690C0","#9970AB")
t3 <- list(family = 'Arial', size = 12)
theme_nyt <- function(){
font <- "Arial" #assign font family up front
theme_bw() %+replace% #replace elements we want to change
theme(
axis.title = element_text( #axis titles
family = font, #font family
size = 12), #font size
axis.text = element_text( #axis text
family = font, #axis famuly
size = 12), #font size,
#grid elements
panel.border= element_blank())+theme(plot.background = element_rect("#FFFFFF"),
legend.title = element_blank(),
legend.text = element_text(family = font, size = 12),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#legend.title=element_blank(),
panel.background = element_rect(fill = "#FFFFFF"),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(linetype = "dashed"),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.x = element_text(angle = 0,size=12, family=font, color = "black"),
axis.text.y = element_text(family=font, face="plain", color="black", size=12, angle=0)) + theme(legend.position = "bottom")
}
#plotly theme
xaxis <- list(title = "",
showline = FALSE,
showgrid = FALSE)
yaxis <- list(title = "",
showgrid = TRUE,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE)
Data Downloaded from https://raw.githubusercontent.com/nychealth/coronavirus-data/master/trends/data-by-day.csv. The data includes case number.
covid_nyc <- read.csv("./COVID-19_Daily_Counts_of_Cases__Hospitalizations__and_Deaths.csv", stringsAsFactors = F)
covid_nyc <- covid_nyc %>% mutate(date_of_interest = mdy(date_of_interest))
full_join (covid_nyc %>% dplyr::select(date_of_interest, DEATH_COUNT) %>%
mutate(type = "Confirmed Covid-19 Death"),
covid_nyc %>% dplyr::select(date_of_interest, DEATH_COUNT = PROBABLE_DEATH_COUNT) %>%
mutate(type = "Assumed Covid-19 Death")) %>%
ggplot(aes(x = date_of_interest, y = DEATH_COUNT, color = type)) +
stat_smooth(method = 'loess',n = 500, span=1/60, se = FALSE) +
scale_color_manual(values= palette1, guide = guide_legend(override.aes = list(shape = c(16, 18)))) +
ggtitle("Covid-19 Deaths \n New York City")+
theme_nyt()+ theme(plot.title = element_text(size=17))
# ggsave("Covid_19_new_york_2020_2023.png", width = 6, height = 4, dpi = 300)
plotly_df_death <- full_join (covid_nyc %>% dplyr::select(date_of_interest, DEATH_COUNT)%>%
mutate(type = "Confirmed Covid-19 Death"),
covid_nyc %>% dplyr::select(date_of_interest, DEATH_COUNT = PROBABLE_DEATH_COUNT) %>%
mutate(type = "Assumed Covid-19 Death"))
plot_ly(x = ~plotly_df_death$date_of_interest, y = ~plotly_df_death$DEATH_COUNT, color = plotly_df_death$type, type = "scatter", mode = "lines", colors = c("#FC4E2A","#BD0026"), line = list(shape = 'line', smoothing = 1), width = 4) %>%
layout(title = list(text = paste0('Covid-19 Deaths',
'<br>',
'<sup>',
'New York City',
'</sup>')),
legend = list(orientation = "h", # show entries horizontally
xanchor = "center", # use center of legend as anchor
x = 0.5), # put legend in center of x-axis
font = list(family = "Arial", size = 17), xaxis = xaxis, yaxis = yaxis, margin = list(t = 80))
full_join (covid_nyc %>% dplyr::select(date_of_interest, CASE_COUNT)%>%
mutate(type = "Confirmed Covid-19 Patient"),covid_nyc %>%
dplyr::select(date_of_interest, CASE_COUNT = PROBABLE_CASE_COUNT) %>%
mutate(type = "Assumed Covid-19 Patient")) %>%
ggplot(aes(x = date_of_interest, y = CASE_COUNT, color = type)) +
stat_smooth(method = 'loess',n = 500, span=1/60, se = FALSE) +
scale_color_manual(values= palette2, guide = guide_legend(override.aes = list(shape = c(16, 18)))) +
scale_y_continuous(expand = c(0,0)) +
ggtitle("Covid-19 Reported Cases \n New York City")+
theme_nyt()+ theme(plot.title = element_text(size=17))
#ggsave("Covid_19_new_york_2020_2023_cases.png", width = 6, height = 4, dpi = 300)
plotly_df_patient <- full_join (covid_nyc %>% dplyr::select(date_of_interest, CASE_COUNT)%>%
mutate(type = "Confirmed Covid-19 Patient"),covid_nyc %>%
dplyr::select(date_of_interest, CASE_COUNT = PROBABLE_CASE_COUNT) %>%
mutate(type = "Assumed Covid-19 Patient"))
plot_ly(x = ~plotly_df_patient$date_of_interest, y = ~plotly_df_patient$CASE_COUNT, color = plotly_df_patient$type, type = "scatter", mode = "lines", colors = c("#045A8D","#3690C0"), line = list(shape = 'line', smoothing = 1), width = 4) %>%
layout(title = list(text = paste0('Covid-19 Reported Cases',
'<br>',
'<sup>',
'New York City',
'</sup>')),
legend = list(orientation = "h", # show entries horizontally
xanchor = "center", # use center of legend as anchor
x = 0.5), # put legend in center of x-axis
font = list(family = "Arial", size = 17), xaxis = xaxis, yaxis = yaxis, margin = list(t = 80))
covid_nyc %>% dplyr::select(date_of_interest, HOSPITALIZED_COUNT) %>%
ggplot(aes(x = date_of_interest, y = HOSPITALIZED_COUNT)) +
stat_smooth(method = 'loess',n = 500, span=1/60, se = FALSE, color = "#9970AB") +
scale_y_continuous(expand = c(0,0)) +
labs(title = "Covid-19 Hospitalizations", subtitle = "New York City") +
theme_nyt()
#ggsave("./final_figures/hospitalizations_covid_19_new_york_2020_2023.png", width = 6, height = 4, dpi = 300)
plotly_df <- covid_nyc %>% dplyr::select(date_of_interest, HOSPITALIZED_COUNT)
plot_ly(x = ~plotly_df$date_of_interest, y = ~plotly_df$HOSPITALIZED_COUNT, type = "scatter", mode = "lines", line = list(shape = 'line', smoothing = 1, color = "#6B5B95",width = 4)) %>%
layout(title = list(text = paste0('Covid-19 Hospitalizations',
'<br>',
'<sup>',
'New York City',
'</sup>')),
font = list(family = "Arial", size = 17), xaxis = xaxis, yaxis = yaxis, margin = list(t = 80))
df = rbind((full_join(covid_nyc %>% dplyr::select(date_of_interest, CASE_COUNT)%>%
mutate(label = "Confirmed Patient"),covid_nyc %>%
dplyr::select(date_of_interest, CASE_COUNT = PROBABLE_CASE_COUNT) %>%
mutate(label = "Assumed Patient")) %>%
mutate(count = CASE_COUNT) %>%
dplyr::select(date_of_interest, count, label)),
full_join(covid_nyc %>% dplyr::select(date_of_interest, DEATH_COUNT) %>%
mutate(label = "Confirmed Death"),covid_nyc %>%
dplyr::select(date_of_interest, DEATH_COUNT = PROBABLE_DEATH_COUNT) %>%
mutate(label = "Assumed Death"))%>% mutate(count =DEATH_COUNT) %>%
dplyr::select(date_of_interest, count, label)) %>%
rbind(covid_nyc %>% dplyr::select(date_of_interest, HOSPITALIZED_COUNT) %>%
mutate(count = HOSPITALIZED_COUNT, label = "Hospitalized") %>%
dplyr::select(date_of_interest, count, label))
# last <- max(df$date_of_interest)
ggplot(data = df, aes(x = (date_of_interest), y = count, color = label, label = label)) +
stat_smooth(method = 'loess',n = 500, span=1/60, se = FALSE)+
scale_color_manual(values= palette3) +
scale_y_sqrt(breaks = c(0,800, 1600, 2400, 4800,10000, 20000, 30000, 40000),
labels = c("0","800", "1600","2400", "4800","10000", "20000", "30000", "40000")) +
labs(title = "Covid-19 Cases", subtitle = "New York City") +
theme_nyt()+theme(legend.position = "none")
#ggsave("./final_figures/all_new_york_2020_2023.png",bg = FALSE, dpi = 300)
CovidDat <- read.csv('https://raw.githubusercontent.com/nychealth/coronavirus-data/master/totals/data-by-modzcta.csv', stringsAsFactors = F)
Neighborhoods <- geojson_sf("./nychealth_coronavirus_data master_Geography_resources/MODZCTA_2010_WGS1984.geo.json")
CovidDat <- CovidDat %>% mutate(DEATH_RATE = (COVID_DEATH_COUNT/COVID_CASE_COUNT)*100)
infern <- inferno(n = 7, direction = -1)
inner_join(Neighborhoods, CovidDat, by = 'label') -> MapData
pal_death_rate <- colorBin(infern, MapData$DEATH_RATE, #or colorNumber
na.color = "pink")
leaflet() %>%
addPolygons(data = MapData,
label = ~NEIGHBORHOOD_NAME,
weight = 1.2,
fillColor = ~pal_death_rate(DEATH_RATE),
fillOpacity = 1,
color = "black") %>%
addLegend(data = MapData,
pal = pal_death_rate,
values = ~DEATH_RATE,
opacity = 1,
title="Covid-19 Death Rate") %>%
setMapWidgetStyle(style = list(background = "white"))